/*****************************************************************************
 * execs prog_filename with stdin/out/err redirected to the given socket 
 * returns value from exec()
 ***/
int NetSockServeProgram(char *prog_filename, int socket, char **arglist)
{
  int   exstatus=0;
  pid_t child=0;
  int   statinfo;
  int   p[2];
  int   loop=0;

  while (arglist[loop]!=NULL)
  { 
    printf("argv[%d]: %s\n", loop, arglist[loop]);
    loop++;
  }
/*
	FILE *fp;
	int ch;
	char *lp;
	int p[2];
#define	ENTRIES	50
	char **ap, *av[ENTRIES + 1], line[1024], *strtok();

	if (!fgets(line, sizeof(line), stdin))
		exit(1);

	av[0] = "finger";
	for (lp = line, ap = &av[1];;) {
		*ap = strtok(lp, " \t\r\n");
		if (!*ap)
			break;
		if (++ap == av + ENTRIES)
			break;
		lp = NULL;
	}
*/
	switch(child=fork())
 {
  	case 0:
	 	  close(p[0]);
 		if (p[1] != 1) {
			  dup2(p[1], 1);
			  close(p[1]);
		}
		execv(prog_filename, arglist);
		_exit(1);
	case -1:
		perror("fork");
	}
	close(p[1]);
	if (!(fp = fdopen(p[0], "r")))
		perror("fdopen");
	while ((ch = getc(fp)) != EOF) {
		if (ch == '\n')
			putchar('\r');
		putchar(ch);
	}
*/
  if ((child=fork())==0)
  {
    fclose(stdin);
    fclose(stdout);
    fclose(stderr);

    dup(socket);
    dup(socket);
    dup(socket);

    execv(prog_filename, arglist);
  }
  else
  {
    close(socket);
    while (wait(&statinfo) != child);

    if (WIFEXITED(statinfo)!=0)
      exstatus=WEXITSTATUS(statinfo);
    else
      exstatus=0;
    return (exstatus);
  }
}


